#此页面用来发布文章保存草稿

from selenium.webdriver.common.by import By

from base.base import BasePage,BaseHandle
from utils import UtilsDriver

#定义对象层
class PublishArticlesPage(BasePage):
    def __init__(self):
        super().__init__()
        # 输入标题
        self.title = By.XPATH, "//*[@id='post-title']"
        #iframe元素对象
        self.iframe_ele = By.ID,"Editor_Edit_EditorBody_ifr"
        # 输入内容
        self.content = By.ID, "mat-input-6"
        # 点击存为草稿按钮
        self.SaveAsDraft_btn = By.LINK_TEXT, " 存为草稿"

    # 定位标题输入框
    def find_title(self):
        return self.get_element(self.title)
    #找切换的iframe
    def find_iframe(self):
        return self.get_element(self.iframe_ele)

        # 定位内容输入框

    def find_content(self):
        return self.get_element(self.content)

        # 定位存草稿按钮

    def find_SaveAsDraft_btn(self):
        return self.get_element(self.SaveAsDraft_btn)



    # 定义操作层
class PublishArticlesHandle(BaseHandle):
        def __init__(self):
            self.PublishArticles_page = PublishArticlesPage()

        # 输入标题
        def input_title(self, title):
            self.input_text(self.PublishArticles_page.find_title(), title)

        # 输入内容
        def input_content(self, content):
            #切换到iframe中
            UtilsDriver.get_driver().switch_to.frame(self.PublishArticles_page.find_iframe())
            self.input_text(self.PublishArticles_page.find_content(), content)
            #切回到默认首页
            UtilsDriver.get_driver().switch_to.default_content()

        # 点击存草稿按钮
        def click_SaveAsDraft_btn(self):
            self.PublishArticles_page.find_SaveAsDraft_btn().click()

    # 定义业务层
class PublishArticlesProxy:
        def __init__(self):
            self.PublishArticles_handle = PublishArticlesHandle()

        # 存草稿发表文章业务
        def PublishArticles(self, title, content):
            # 输入标题
            self.PublishArticles_handle.input_title(title)
            # 输入内容
            self.PublishArticles_handle.input_content(content)
            # 点击存草稿按钮
            self.PublishArticles_handle.click_SaveAsDraft_btn()